// 正则规则
export const rules = {
  required: /.+/,
  number: /^[0-9]+$/,
  phoneOrEmail: /(^1[0-9]{10}$)|(^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$)/,
  phone: /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/
}

// 根据规则校验字符串是否有效
export const isValid = (rule, string) => {
  let isValid = false
  if (typeof rules[rule] !== 'undefined') {
    isValid = rules[rule].test(string)
  } else if (typeof rule === 'object') {
    const params = typeof string === 'string' ? string.replace(' ', '') : string
    isValid = rule.test(params)
  }
  return isValid
}

export const doValid = (rule, string, id, context, reassign) => {
  if (!context) context = this
  if (!context.validateList) context.validateList = {}
  const isValidText = isValid(rule, string)
  if (reassign === false) {
    context.validateList = Object.assign({}, context.validateList, { [id]: isValidText })
  } else {
    console.log(rule, string, isValidText)
    context.validateList[id] = isValidText
  }
  return isValidText
}

export const isAllValid = (context) => {
  if (!context) context = this
  let isValidForAll = true
  let isValidText = true
  console.log(11111, context.validateList && Object.keys(context.validateList))
  if (context.validateList && Object.keys(context.validateList).length) {
    for (const id in context.validateList) {
      if (isValidText === true) {
        isValidText = context.validateList[id]
        if (isValidText === false) isValidForAll = false
      }
    }
  }
  return isValidForAll
}
